package com.uenpay.im.weight;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.uenpay.im.R;
import com.uenpay.im.adapter.RotaryPhotoAdapter;
import com.uenpay.im.entities.RotateImageViewEntity;

import java.util.ArrayList;


/**
 * Copyright http://www.qiyunxin.com
 * 
 * @author SL
 *
 */
public class MyViewPage {

	private IRotaryPhotoListener callBackListener;
	private Context mContext;
	private LinearLayout business_navi_bottom_layout;
	private LinearLayout viewGroup;
	private RotaryPhotoAdapter rotary_adapter;
	private ViewPager viewpager;
	private Runnable runnable;
	private int autoChangeTime = 5000;
	private ImageView dot, dots[];
	private int layout_bg_color;
	private ArrayList<RotateImageViewEntity> RotationList;

	public MyViewPage(Context _Context, ArrayList<RotateImageViewEntity> _list,
			int layout_bg_color, IRotaryPhotoListener _callBackListener) {
		mContext = _Context;
		RotationList = _list;
		this.layout_bg_color = layout_bg_color;
		callBackListener = _callBackListener;

	}

	public View getView() {
		LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
		FrameLayout headerLayout = (FrameLayout) mLayoutInflater.inflate(
				R.layout.layout_rotate_photo_header, null);
		business_navi_bottom_layout = (LinearLayout) headerLayout
				.findViewById(R.id.business_navi_bottom_layout);
		business_navi_bottom_layout.setBackgroundResource(layout_bg_color);
		viewGroup = (LinearLayout) headerLayout.findViewById(R.id.viewGroup);
		viewpager = (ViewPager) headerLayout.findViewById(R.id.viewpager);
		rotary_adapter = new RotaryPhotoAdapter(mContext, callBackListener);
		rotary_adapter.addData(RotationList);

		viewpager.setAdapter(rotary_adapter);

		viewpager.setOnPageChangeListener(myOnPageChangeListener);

		if (RotationList.size() > 1) {
			initDot();
		}

		runnable = new Runnable() {
			@Override
			public void run() {
				int next = viewpager.getCurrentItem() + 1;
				if (next >= rotary_adapter.getCount()) {
					next = 0;
				}
				viewHandler.sendEmptyMessage(next);
			}
		};
		if (RotationList.size() > 1) {
			viewHandler.postDelayed(runnable, autoChangeTime);
		}

		return headerLayout;
	}

	public void SetViewpagerHeight(int val) {
		LayoutParams layoutparam = viewpager.getLayoutParams();
		layoutparam.height = val;
		viewpager.setLayoutParams(layoutparam);
	}

	// ��ʼ��dot��ͼ
	private void initDot() {
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		layoutParams.setMargins(6, 3, 6, 3);

		dots = new ImageView[rotary_adapter.getCount()];
		for (int i = 0; i < rotary_adapter.getCount(); i++) {
			dot = new ImageView(mContext);

			dot.setLayoutParams(layoutParams);
			dots[i] = dot;
			dots[i].setTag(i);
			dots[i].setOnClickListener(onClick);

			if (i == 0) {
				dots[i].setBackgroundResource(R.drawable.point_selected);
			} else {
				dots[i].setBackgroundResource(R.drawable.point);
			}

			viewGroup.addView(dots[i]);
		}
	}

	OnPageChangeListener myOnPageChangeListener = new OnPageChangeListener() {

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int arg0) {
			setCurDot(arg0);
			viewHandler.removeCallbacks(runnable);
			viewHandler.postDelayed(runnable, autoChangeTime);
		}
	};

	/**
	 * ѡ�е�ǰ����С��
	 */
	private void setCurDot(int position) {
		for (int i = 0; i < dots.length; i++) {
			if (position == i) {
				dots[i].setBackgroundResource(R.drawable.point_selected);
			} else {
				dots[i].setBackgroundResource(R.drawable.point);
			}
		}
	}

	/**
	 * ÿ���̶�ʱ���л������ͼƬ
	 */
	@SuppressLint("HandlerLeak")
	private final Handler viewHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			setCurView(msg.what);
		}
	};

	/**
	 * ���õ�ǰ������ҳ
	 */
	private void setCurView(int position) {
		if (position < 0 || position > rotary_adapter.getCount()) {
			return;
		}
		viewpager.setCurrentItem(position);
	}

	// ʵ��dot�����Ӧ����
	OnClickListener onClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			int position = (Integer) v.getTag();
			setCurView(position);
		}

	};

	public interface IRotaryPhotoListener {
		public void onClicked(RotateImageViewEntity rotateEntity, int index);
	}
}
